package sendsms.example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class SendSMSExampleActivity extends Activity {
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms);
		final EditText tlf = (EditText) findViewById(R.id.NumeroTelefono);
		final EditText sms = (EditText) findViewById(R.id.TextoSms);
		Button boton = (Button) findViewById(R.id.BotonEnviar);
		boton.setOnClickListener(new OnClickListener() {
		
			@Override
			public void onClick(View v) {
				String destino = tlf.getText().toString();
				String texto = sms.getText().toString();
				enviaSMS(destino, texto);
				tlf.setText("");
				sms.setText("");
			}
		});
		
	}
	
	private void enviaSMS(String destino, String texto) {
	
		try {
			// El envío propiamente dicho
			SmsManager smsMgr = SmsManager.getDefault();
			smsMgr.sendTextMessage(destino, null, texto, null, null);
			// 	Avisamos al usuario
			Toast.makeText(SendSMSActivity.this, "SMS enviado", Toast.LENGTH_LONG).show();
		} 
		catch (Exception e) {
			Toast.makeText(SendSMSActivity.this, "Error en el envio: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
		}
	}
	
}